import weka
import weka.core.Instance as Instance
import weka.core.Instances as Instances
import weka.clusterers.SimpleKMeans as KMeans
from Matrix import *
import MatrixIO
import urllib

def SaveARFF(X):
	pass

class PyStrAttribute(weka.core.Attribute):
	m_Type=None
	def __init__(self,name):
		weka.core.Attribute.__init__(self,name)
		self.m_Type=weka.core.Attribute.STRING
		self.m_Values=weka.core.FastVector()
		self.m_Index = 0;
		self.m_Hashtable = java.util.Hashtable()

def MakeWEKAStructure(d,dlabels,explabels):
	[r,c]=size(d)
	gene = weka.core.Attribute("gene")
	values = []
#	gene.m_Type=weka.core.Attribute.STRING#
#	gene.m_Values=weka.core.FastVector()
#	gene.m_Index = 0;
#	gene.m_Hashtable = java.util.Hashtable()   # need to rewrite constructor

	for m in explabels:
		values += [weka.core.Attribute(m)]

	print values
	v = weka.core.FastVector(c+1)
	v.addElement(gene)
	for m in values:
		v.addElement(m)
	I = Instances('matrix',v,r)
	for i in xrange(0,r):
		inst = Instance(c+1)
		inst.setValue(v.elementAt(0),i)
		iterv=v.elements(0)
		for j in range(0,c):
			#print d[i+1,j+1]
			z = iterv.nextElement()
			#print z
			inst.setValue(z,d[(i+1),(j+1)])
		I.add(inst)
		inst=None
	I.setClass(gene)
	return I

def kmeans(I,n):
	k = KMeans()
	k.setNumClusters(n)
	f = weka.filters.unsupervised.attribute.Remove();
	f.setAttributeIndices("" + (I.classIndex() + 1).__str__());
	f.setInputFormat(I);
	Q = weka.filters.Filter.useFilter(I, f);
	k.buildClusterer(Q);
	return k

def SMO(I):
	k= SMO()
	k.buildClassifier(I)
	return k
	#distributionForInstance(Instance inst) 

def EMcluster(I,n=None):
	k = EM()
	if n is not None:
		k.setNumClusters(n)
	k.buildClusterer(I)
	return k


def loadFromCache(filename):
	pass

def loadGoFromWeb(urlstr):

	url = java.net.url(urlstr)
	go_reader = java.io.BufferedInputStream(url.openStream()) 
	# = go_reader.readLine()
	# feed this to the CSV reader so it can be parsed, annotated into arff
	
	pass

def ReplaceMissingValues(I):
	f = weka.filters.unsupervised.attribute.ReplaceMissingValues();
	f.setAttributeIndices("" + (I.classIndex() + 1).__str__());
	f.setInputFormat(I);
	Q = weka.filters.Filter.useFilter(I, f);
	return Qa



[d,dl,el]=MatrixIO.readmatrix('Etest.txt')
I = MakeWEKAStructure(d,dl,el);

#q = weka.clusterers.ClusterEvaluation()
#k=kmeans(I,4)
#q.setClusterer(k)
#q.evaluateClusterer(I)
#print dir(q)
#print q.getClusterAssignments()   # create a way to visualize the clusters
#print k.getSquaredError()
#print k.getClusterSizes(), 
#print q.getClassesToClusters() 

urlhandle = urllib.urlretrieve('http://cvsweb.geneontology.org/cgi-bin/cvsweb.cgi/go/gene-associations/gene_association.sgd.gz?rev=HEAD','test.gz')

#java.util.zip.ZipFile
#indata =java.util.zip.GZIPInputStream(java.io.FileInputStream('test.gz'))



w = weka.core.converters.CSVLoader()
w.setSource(java.io.File('gene_association.sgd'))
#print w.getFileDescription()
I = w.getStructure()
print I
# create a cache and store our data
#print dir(I.m_Instances)
#print I.m_Instances.size()
#jf = javax.swing.JFrame()
#wp = weka.gui.explorer.ClassifierPanel()   ### need to post a panel to assign a nominal category. at least for a training set.
#wp = weka.gui.explorer.ClustererPanel()
#wp.setInstances(I)
#jf.add(wp)
#jf.pack()
#jf.show()
